﻿using System;
using System.Collections.Generic;
using System.Linq;
using Store.WP7.Data.WebStore;
using Store.WP7.Framework.Helpers;

namespace Store.WP7.ViewModels
{
    public class StoreCache
    {
        #region Singleton

        /// <summary>
        /// Configuration manager unique instance
        /// </summary>
        private static volatile StoreCache instance;

        /// <summary>
        /// Sync object
        /// </summary>
        private static object syncRoot = new Object();

        #region Instance Property

        /// <summary>
        /// Configuration manager instance
        /// </summary>
        public static StoreCache Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new StoreCache();
                    }
                }

                return instance;
            }
        }

        #endregion

        #endregion

        private StoreCache()
        {
            Products = new List<Product>();
            Orders = new List<Order>();
        }

        public List<Product> Products { get; private set; }

        public List<Order> Orders { get; private set; }

        public void UpdateProducts(IEnumerable<Product> products)
        {
            var productsNotInCache = products.Except(Products, new LambdaComparer<Product>((p1, p2) => p1.Id == p2.Id));

            Products.AddRange(productsNotInCache);
        }

        public void UpdateOrders(IEnumerable<Order> orders)
        {
            var ordersNotInCache = orders.Except(Orders, new LambdaComparer<Order>((o1, o2) => o1.Id == o2.Id));

            Orders.AddRange(ordersNotInCache);
        }
    }
}
